package com.cet.algorithm.二叉树.层级遍历;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @program: algorithm
 * @description: 二叉树的最小深度
 * @author: 陈恩涛
 * @create: 2022-07-18 16:27
 **/
public class LC111 {

    public static void main(String[] args) {

    }

    public int minDepth(TreeNode root) {
        int depth = 0;
        if (root == null) {
            return 0;
        }
        final Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int len = queue.size();
            depth++;
            while (len > 0) {
                final TreeNode cur = queue.poll();
                // 叶子节点判定
                if (cur.left == null && cur.right == null) {
                    return depth;
                }
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
                len--;
            }
        }
        return depth;
    }
}
